package com.yuanfeng.action.front.user.dao;

import com.yuanfeng.pojo.Users;
import com.yuanfeng.util.JdbcUtil;
import com.yuanfeng.util.MD5Util;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl extends HibernateDaoSupport implements IUserDao {

    /**
     * 通过名字查找用户
     * @param hql
     * @param params
     * @return
     */
	public List<Users> findByName(String hql,Object... params) {

		return getHibernateTemplate().find(hql,params);
	}

    /**
     * 保存用户
     * @param user
     */
	public void save(Users user) {
		user.setPassword(MD5Util.md5(user.getPassword().trim()));
		this.getHibernateTemplate().save(user);
	}

	/**
	 * 根据随机验证码查询用户
	 * @param code
	 * @return
	 */
	public Users queryUser(String code){
		try {
			QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
			return (Users) qr.query("select * from users where validatecode=?",new BeanHandler(Users.class), new Object[]{code});
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}

	}
}
